package com.aaa.tmr.service.impl;

import com.aaa.tmr.entity.User;
import com.aaa.tmr.dao.UserDao;
import com.aaa.tmr.entity.Visit;
import com.aaa.tmr.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author makejava
 * @since 2021-03-18 19:16:21
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    @Override
    public PageInfo queryAll(int pageNo, int pageSize, User user) {
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<User>(this.userDao.queryAll(user));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }

    @Override
    public int deleteByIds(String userIds) {
        return this.deleteByIds(userIds);
    }

    /**
     * 通过用户名查询用户
     * @param  userName  用户名
     * @return 用户
     */
    @Override
    public User queryByUserName(String userName) {
        return userDao.queryByUserName(userName);
    }
    @Override
    public List<Map<String, Object>> findAllDept() {
        return userDao.findAllDept();
    }

    @Override
    public List<Map<String, Object>> findAllRole() {
        return userDao.findAllRole();
    }
    /**
     * @return
     * @create by: Back
     * @description: 添加用户权限
     * @create time: 2020/12/24 10:14
     */
    @Override
    public void insertUserRole(int userId, String[] roles) {
        for (int i = 0; i < roles.length; i++) {
            userDao.insertUserRole(userId, roles[i]);
        }
    }

    /**
     * 通过loginName查询单条数据
     *
     * @param loginName
     * @return 实例对象
     */
    @Override
    public User queryByLoginName(String loginName) {
        return this.userDao.queryByLoginName(loginName);
    }
}
